/* jshint esversion: 6 */
const Router = require('koa-router');
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 修改密码
router.post('/user/getDataAuth', async ctx => {
    let userId = ctx.request.body.userId
    let tid = ctx.user.tenantId
    let sql = `select id,name from admin where tenantId=${tid} and isDel=0 `
    let admins = await db.query(sql, {type: QueryTypes.SELECT})
    let hasAuth = await db.query(`select * from admin_data_ower where selfId=${userId} `, {type: QueryTypes.SELECT})
    let authIds = []
    hasAuth.map(row => {
        authIds.push(row.owerId)
    })
    let userList = []
    admins.map(row => {
        let obj = {checked: false, id: row.id, name: row.name}
        if(authIds.indexOf(row.id) !== -1) {
            obj.checked = true
        }
        userList.push(obj)
    })
    let sqld = `select id,name, pid from dept where tenantId=${tid} `
    let depts = await db.query(sqld, {type: QueryTypes.SELECT})
    let deptsAuth = await db.query(`select * from admin_data_dept where selfId=${userId}`, {type: QueryTypes.SELECT})
    let deptList = []
    let deptIds = []
    deptsAuth.map(row => {
        deptIds.push(row.deptId)
    })
    depts.map(row => {
        let obj = {
            id: row.id,
            name: row.name,
            pid: row.pid,
            checked: false
        }
        if(deptIds.indexOf(row.id) !== -1) {
            obj.checked = true
        }
        deptList.push(obj)
    })
    let allData = await db.models['admin'].findOne({
        where: {
            id: userId
        }
    })
    return ctx.suc('', {userList, deptList, allData: allData.allData === 1, selfData: allData.selfData === 1})
})


module.exports = router;